$(function() {
    $("#sign-btn").click(function(){
        clear();
        var username = $("#username").val();
        var mobile = $("#mobile").val().toString();
        var role = $('#role option:selected').val();
        var password = $("#password").val();
        var password_check = $("#password-check").val();
        // 验证用户名
        if(username == ''){
            $("#username-warning").html("用户名不能为空！");
            return;
        }
        var regular = /^[a-zA-Z]{1,30}$/;
        if(!regular.exec(username)){
            $("#username-warning").html("用户名只能包含字母！");
            return;
        }

        // 验证手机号
        if(mobile == ''){
            $("#mobile-warning").html("手机号不能为空！");
            return;
        }
        regular = /^[0-9]{10,12}$/;
        if(!regular.exec(mobile)){
            $("#mobile-warning").html("手机号码为11位数字！");
            return;
        }
        if(mobile.length < 11){
            $("#mobile-warning").html("手机号码有误！");
            return;
        }

        // 验证角色
        if(role == ''){
            $("#role-warning").html("角色不能为空！");
            return;
        }

        // 验证密码
        if(password == ''){
            $("#password-warning").html("密码不能为空！");
            return;
        }
        regular = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){5,19}$/;
        if(!regular.exec(password)){
            $("#password-warning").html("密码6-18位，以字母开头，只能包含字母数字和下划线！");
            return;
        }

        if(password != password_check){
            $("#password-check-warning").html("密码不一致！");
            return;
        }

        if(!$("#customCheck1").is(":checked")){
            $("#check-warning").html("请先同意协议！");
            return;
        }
        $.ajax({
            type:"post",
            url: "/work/signUp",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify({
                "userName": username,
                "mobile": mobile,
                "role": role,
                "password": password
            }),
            success: function (result) {
                dealResult(result);
            },
            error: function (){
                $("#warning-fade").html("服务请求失败！");
                $("#myModal").modal();
            }
        });
    });

    function dealResult(result) {
        if(result.statusNum == 200){
            window.location.href="login.html";
        }
        if(result.statusNum == 501){
            $("#username-warning").html(result.statusInfo);
        }
        if(result.statusNum == 502){
            $("#mobile-warning").html(result.statusInfo);
        }
    }

    function clear() {
        $("#username-warning").html("");
        $("#mobile-warning").html("");
        $("#role-warning").html("");
        $("#password-warning").html("");
        $("#password-check-warning").html("");
        $("#check-warning").html("");
    }

});